package leetcode_acm.backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月23日 15:16
 */
public class SubsetsWithDup {
    private List<List<Integer>> list = new ArrayList<>();
    private boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        used = new boolean[nums.length];
        Arrays.sort(nums);
        backtrack(nums,0,0,new ArrayList<>());
        return list;
    }
    private void backtrack(int[] nums,int startIndex,int length,List<Integer> path){
        list.add(new ArrayList<>(path));
        //开始递归回溯
        for(int i = startIndex;i < nums.length;i++){
            //先判断有没有树层重复
            if(i > 0 && nums[i] == nums[i-1] && !used[i-1]){
                continue;
            }
            //否则的话就做出选择
            used[i] = true;
            path.add(nums[i]);
            //递归回溯
            backtrack(nums,i,length+1,path);
            path.remove(length);
            used[i] = false;
        }
    }
}
